package com.ys.shop.dao;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ys.shop.db.IDB;
import com.ys.shop.db.MySqlDBImp;
import com.ys.shop.vo.Users;
import com.ys.shop.vo.VO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDAOImp implements IDAO {
    //登录
    public boolean login(VO vo)throws SQLException{
        //获取数据库连接
        IDB db=new MySqlDBImp();
        Connection conn=db.getConn();
        //使用工具
        String sql="select * from easy_shop where eu_user_name=? and where eu_password=?";
        PreparedStatement ps=conn.prepareStatement(sql);
        Users users=(Users)vo;//强制转换为vo
        ps.setString(1,users.getEu_user_name());
        ps.setString(2,users.getEu_password());
        //使用工具查询
        ResultSet rs=ps.executeQuery();
        while(rs.next()){
            return true;
        }
        return false;
    };
    //注册
    public boolean insert(VO vo) throws SQLException {
        IDB db=new MySqlDBImp();
        Connection conn=db.getConn();
        PreparedStatement ps=conn.prepareStatement("insert into easybuy_user(eu_user_id,eu_user_name,eu_password) values (?,?,?)");
        Users users=(Users)vo;
        ps.setString(1,users.getEu_user_id());
        ps.setString(2,users.getEu_user_name());
        ps.setString(3,users.getEu_password());
        int i=ps.executeUpdate();
        ps.close();
        db.closegetConn();
        return  i>0;
    }

    @Override
    public List getAll() throws SQLException {
        PreparedStatement preparedStatement=null;
        ResultSet rs=null;
        List<VO> list=new ArrayList<VO>();
        IDB db=new MySqlDBImp();
        Connection conn=null;
        try {
            conn=db.getConn();
            String sql="select * from easybuy_user";
            //得到数据库
            preparedStatement=conn.prepareStatement(sql);
            //
            rs=preparedStatement.executeQuery();
            while (rs.next()){
                Users u=new Users();
                u.setEu_user_id(rs.getString(1));
                u.setEu_user_name(rs.getString(2));
                u.setEu_address(rs.getString(9));
                u.setEu_birthday(rs.getString(5));
                u.setEu_email(rs.getString(7));
                u.getEn_identity_code(rs.getString(6));
                u.getEn_mobile(rs.getString(8));
                u.setEu_password(rs.getString(3));
                u.setEu_sex(rs.getString(4));
                u.getEu_status(rs.getInt(10));
                list.add(u);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            db.closeAll(preparedStatement,rs,null);
        }
        return list;
    }

    @Override
    public boolean deleteByID(VO vo) throws SQLException {
        return false;
    }

    @Override
    public boolean update(VO vo) throws SQLException {
        return false;
    }

    @Override
    public VO getByID(VO vo) throws SQLException {
        return null;
    }

    public static void main(String[] args){
            UserDAOImp daoImp=new UserDAOImp();
        try {
            //将LIST转换为JSON
            List<VO> listall= daoImp.getAll();
            String j=JSON.toJSONString(listall);
            System.out.println(j);
            //将
            List<Users> testlist=JSONObject.parseArray(j,Users.class);
            System.out.println(testlist.size()+"asdffdas");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

